import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/model/product.dart';

class GoodsListItemWidget extends StatelessWidget {
  Product goods;
  double width;

  GoodsListItemWidget(this.goods, this.width);

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        width: width,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            CachedNetworkImage(
              width: width,
              height: width,
              imageUrl: goods.avatar,
              fit: BoxFit.fill,
            ),
            Expanded(
              child: Container(
                padding: const EdgeInsets.only(
                    top: 4.0, bottom: 4.0, left: 4.0, right: 4.0),
                child: Text(
                  goods.name ?? "",
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 14.0, color: ColorCommon.black1),
                ),
              ),
            ),
            Container(
              padding: const EdgeInsets.only(left: 4.0, right: 4.0),
              child: Row(children: <Widget>[
                goods.directSale != null && goods.directSale
                    ? Padding(
                        padding: const EdgeInsets.only(right: 4.0),
                        child: _getTagView("自营", Colors.white, ColorCommon.primaryColor,
                            ColorCommon.primaryColor))
                    : Container(),
                goods.promotion != null && goods.promotion
                    ? Padding(
                        padding: const EdgeInsets.only(right: 4.0),
                        child: _getTagView("促销价", Colors.white,
                            ColorCommon.primaryColor, ColorCommon.primaryColor),
                      )
                    : Container(),
                Expanded(
                  child: Text(
                    "98.0%好评",
                    style: TextStyle(fontSize: 14.0, color: ColorCommon.grey8),
                  ),
                ),
              ]),
            ),
            Container(
              padding: const EdgeInsets.only(
                  top: 4.0, bottom: 4.0, left: 4.0, right: 4.0),
              child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: <Widget>[
                    Text(
                      "¥" +
                          (goods.promotion != null && goods.promotion
                              ? (goods.lowPromotionPrice ?? "0.0")
                              : (goods.lowSalePrice ?? "0.0")),
                      style: TextStyle(fontSize: 18.0, color: ColorCommon.primaryColor),
                    ),
                    goods.promotion != null && goods.promotion
                        ? Expanded(
                            child: Padding(
                              padding: const EdgeInsets.only(left: 4.0),
                              child: Text(
                                "¥" + (goods.lowSalePrice ?? "0.0"),
                                style: TextStyle(
                                  fontSize: 14.0,
                                  color: ColorCommon.grey8,
                                  decoration: TextDecoration.lineThrough,
                                  decorationStyle: TextDecorationStyle.solid,
                                ),
                              ),
                            ),
                          )
                        : Container(),
                  ]),
            ),
          ],
        ));
  }

  Widget _getTagView(
      String text, Color bgColor, Color borderColor, Color textColor,
      {DecorationImage image}) {
    BoxDecoration decoration;
    if (image == null) {
      decoration = BoxDecoration(
          color: bgColor,
          border: Border.all(color: borderColor),
          borderRadius: BorderRadius.all(Radius.circular(100.0)));
    } else {
      decoration = BoxDecoration(image: image);
    }
    return Container(
      padding: const EdgeInsets.only(left: 4.0, right: 4.0),
      decoration: decoration,
      child: Text(
        text,
        style: TextStyle(fontSize: 13.0, color: textColor),
      ),
    );
  }
}
